package com.hm.ibsm.web;

import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hm.ibsm.manage.vo.role.RoleVo;
import com.hm.ibsm.service.RoleService;
import com.hm.ibsm.util.BaseController;

import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 2017年3月12日 下午10:44:41
 * @version 1.0
 * @describe
 */
@Slf4j
@Controller
@RequestMapping("/mg/role")
public class RoleController extends BaseController {

	@Autowired
	private RoleService roleService;

	private String MODEL_NAME = UserController.class.getName();

	@RequestMapping(value = "/index.do", method = RequestMethod.GET)
	public String roleMg() {
		return "mg/role";
	}

	@RequestMapping(value = "/add.do", method = RequestMethod.GET)
	public String addRole() {
		return "mg/role/add";
	}

	@RequestMapping(value = "/detail.do", method = RequestMethod.GET)
	public ModelAndView detailRole(RoleVo role) {
		try {
			HashMap<String, Object> result = new HashMap<String, Object>();
			result.put("result", roleService.findRole(role.getId()));
			return new ModelAndView("mg/role/detail", result);
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
			return sessionTimeOut();
		}
	}

	@RequestMapping(value = "/update.do", method = RequestMethod.GET)
	public ModelAndView updateRole(RoleVo role) {
		try {
			HashMap<String, Object> result = new HashMap<String, Object>();
			result.put("result", roleService.findRole(role.getId()));
			return new ModelAndView("mg/role/update", result);
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
			return sessionTimeOut();
		}
	}

	@RequestMapping(value = "/addRole.do", method = RequestMethod.POST)
	public String saveAddRole(RoleVo role) {
		try {
			roleService.addRole(role);
			return roleMg();
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
			return SESSION_TIME_OUT_URL;
		}
	}

	@RequestMapping(value = "/delete.do", method = RequestMethod.GET)
	public String saveDeleteRole(RoleVo role) {
		try {
			roleService.deleteRole(role.getId());
			return roleMg();
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
			return SESSION_TIME_OUT_URL;
		}
	}

	@RequestMapping(value = "/updateRole.do", method = RequestMethod.POST)
	public String saveUpdateRole(RoleVo role) {
		try {
			roleService.updateRole(role.getId(), role);
			return roleMg();
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
			return SESSION_TIME_OUT_URL;
		}
	}

	@RequestMapping(value = "/query.do", method = RequestMethod.GET)
	public @ResponseBody String query(RoleVo role) {
		return json(roleService.query(1, 10, role)).toString();
	}
}
